
g_input_dir="C:/Users/ren067/Data/R/input/"
g_output_dir="C:/Users/ren067/Data/R/output/"
g_8099ave_dir=paste(g_output_dir,"8099ave", sep="")
g_2039ave_dir=paste(g_output_dir,"2039ave/", sep="")
g_change_dir=paste(g_output_dir,"change/", sep="")

setwd(g_basedir)

files=c("BCM2.0")
ss=c("B1")

years=c(1980:2039)

print("######################################################################################")
for(i in files) {
  for (s in ss) {
    for (j in 1:9) {
      
      file_path = filename(i,j,s)
      print(file_path)
      if(file.exists(file_path)) {
        base=read.csv(filename(i,j,s),header=TRUE,sep=",")
        base=nswbase(base)
        
        if(s=="C20C") {
          base=subset(base,year>=1980 & year<=1999)
        }else {
          base=subset(base,year>=2020 & year<=2039)
        }	
        
        
        for(k in years)
        {
          print(k)
          
          bssub=basesub(base,k)
          bssub1=bssub[,4:20]
          nr=nrow(bssub1)
          basetot=matrix(0:0,nrow=nr,ncol=17)
          if(k==2020||k==2019)
          {
            savefile(basetot, paste(g_output_dir,"basetot-create-",c(k), ".csv",sep=""));
          }
          basetot=basetot+bssub1
          if(k==2020||k==2019)
          {
            savefile(bssub1, paste(g_output_dir,"bssub1-plus-",c(k), ".csv",sep=""));
            savefile(basetot, paste(g_output_dir,"basetot-plus-",c(k), ".csv",sep=""));
          }
          
          ftave=basetot/20
          
          if(k==2020||k==2019)
          {
            print(paste("k==", c(k), "[", c(nr),"]",sep=""))
            savefile(base, paste(g_output_dir,"base-",c(k), ".csv",sep=""));
            savefile(bssub, paste(g_output_dir,"bssub-",c(k), ".csv",sep=""));
            savefile(bssub1, paste(g_output_dir,"bssub1-",c(k), ".csv",sep=""));
            savefile(basetot, paste(g_output_dir,"basetot-",c(k), ".csv",sep=""));
            #savefile(bsave, paste(g_output_dir,"basetot-",c(k), ".csv",sep=""));  
          }
          
          
          if(s=="C20C") {
            print(paste("k=",c(k),"  ,  bsave",sep=""))
            bsave=basetot/20
          }else {
            print(paste("k=",c(k),"  ,  ftave",sep=""))
            ftave=basetot/20
          }
          
          if(k==2020)        
          {
            print("k==2020")
            
            savefile(bsave, paste(g_output_dir,"bsave.csv",sep=""));
            savefile(ftave, paste(g_output_dir,"ftave.csv",sep=""));
          }
          
          change=100*(ftave-bsave)/bsave
          
        }
        

      }
    }
  }
}
print("Over")

#identify filename
filename=function(ii,jj,st){
  fn=paste(c(g_basedir),ii,
           ("_Run"),jj,("_"),st,("_Rain.csv"),sep="")
  return(fn)
}

#subset nswbase

nswbase=function(gcm){
  nswb=subset(gcm,lat>=-39 & lat<=-27 & lon>=139 &lon<=155 )
  return(nswb)
}

# identify rows for one year

nrf=function(gcm){
  nrows=nrow(subset(gcm,year==min(gcm[,3])))
  return(nrows)
}

# select two different period

#base=function(gcm){
#	if(s=="C20C") {
#		bs=subset(gcm,year>=1980 & year<=1999)
#		}
#	else {
#		bs=subset(gcm,year>=2020 & year<=2039)
#		}
#	return(bs)
#}

#identify fileoutave

fileoutave=function(gcm,ii,jj,sv){
  if(sv=="C20C"){
    fn= paste(c(g_8099ave_dir),ii,"_",sv,
              ("_run"),jj,"_",("8099ave.csv"),sep="")
  }
  else {
    fn= paste(c(g_2039ave_dir),ii,"_",sv,
              ("_run"),jj,"_",("2039ave.csv"),sep="")
  }
  fileout=write.table(gcm,fn,quote=FALSE, sep=",",row.names=FALSE)
  return(fileout)
  
}

#fileoutnsw=function(gcm,ii,jj,ss){
#	fn=paste(c("D:/Environmental Trust/GCM/2039/"),ii,"_",ss,
#			("_run"),jj,"_",("nsw.csv"),sep="")
#	fileout=write.table(gcm,fn,quote=FALSE, sep=",",row.names=FALSE)
#	return(fileout)
#
#}


#identify fileoutchange

fileoutchg=function(gcm,ii,jj,sv){
  
  fn= paste(c(g_change_dir),ii,"_",sv,
            ("_run"),jj,"_",("change.csv"),sep="")
  fileout=write.table(gcm,fn,quote=FALSE, sep=",",row.names=FALSE)
  return(fileout)
}

#identify baseline average and future avarage

basesub=function(gcm,kk){
  
  #	bsub=gcm[(kk*nrf(gcm)+1):(kk*nrf(gcm)+nrf(gcm)),4:20]
  bsub=subset(gcm,year==kk)
  return(bsub)
}

savefile=function(fh, file_path)
{
  fn= file_path
  fileout=write.table(fh,fn,quote=FALSE, sep=",",row.names=FALSE)
  return(savefile)
}

